<?php 
/*
 module:		举报管理
 create_time:	2022-05-20 10:38:47
 author:		
 contact:		
*/

namespace app\backend\controller;

use app\backend\service\ZhReportService;
use app\backend\model\ZhReport as ZhReportModel;
use think\facade\Db;

class ZhReport extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$where = [];
			$where['a.wxapp_id'] = session('backend.wxapp_id');
			$where['a.status'] = $this->request->param('status', '', 'serach_in');

			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

			$field = 'a.*,b.nickname as bnickname,c.nickname as cnickname';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'a.id desc';

			$res = ZhReportService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
			
			return json($res);
		}
	}

	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$id = $this->request->get('id','','serach_in');
			if(!$id) $this->error('参数错误');
			$res = checkData(ZhReportModel::find($id));
			$res['unickname'] = \app\api\model\Member::where(['wxapp_id'=> session('backend.wxapp_id'),'uid'=> $res['u_id']])->value('nickname');
            $res['rnickname'] = \app\api\model\Member::where(['wxapp_id'=> session('backend.wxapp_id'),'uid'=> $res['r_id']])->value('nickname');
            $res['image'] = explode(',',$res['image']);
            
			$this->view->assign('info',$res);
			return view('update');
		}else{
			$postField = 'id,wxapp_id,u_id,r_id,content,image,status,createtime';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = ZhReportService::update($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

	/*删除*/
	function delete(){
		$idx =  $this->request->post('id', '', 'serach_in');
		if(!$idx) $this->error('参数错误');
		try{
			ZhReportModel::destroy(['id'=>explode(',',$idx)],true);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*查看详情*/
	function view(){
		$id = $this->request->get('id','','serach_in');
		if(!$id) $this->error('参数错误');
		$res = ZhReportModel::find($id);
        $res['image'] = explode(',',$res['image']);
		$this->view->assign('info',$res);
		return view('view');
	}



}

